package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.TestSoul;
import com.itheima.domain.db.TestSoulLock;
import com.itheima.domain.db.TestSoulQuestion;
import com.itheima.domain.db.TestSoulQuestionsOption;
import com.itheima.domain.mongo.TestScore;
import com.itheima.mapper.TestSoulLockMapper;
import com.itheima.mapper.TestSoulMapper;
import com.itheima.mapper.TestSoulQuestionMapper;
import com.itheima.mapper.TestSoulQuestionsOptionMapper;
import com.itheima.service.db.TestSoulQuestionsOptionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@DubboService
public class TestSoulQuestionsOptionServiceImpl  implements TestSoulQuestionsOptionService {

    @Autowired
    private TestSoulQuestionsOptionMapper testSoulQuestionsOptionMapper;

    @Autowired
    private TestSoulQuestionMapper testSoulQuestionMapper;
    @Autowired
    private TestSoulLockMapper testSoulLockMapper;
    @Autowired
    private TestSoulMapper testSoulMapper;
    @Override
    public TestScore sumScore(Map<String, String>[] answer, Long userId) {
        TestScore testScore = new TestScore();
        Integer sum=0;
        String questionId=null;
        String optionId=null;
        for (Map<String, String> map : answer) {
            questionId = map.get("questionId");
            optionId = map.get("optionId");
            LambdaQueryWrapper<TestSoulQuestionsOption> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TestSoulQuestionsOption::getId,optionId).eq(TestSoulQuestionsOption::getTestSoulQuestionId,questionId);
            TestSoulQuestionsOption option = testSoulQuestionsOptionMapper.selectOne(wrapper);
            sum+=option.getScore();
        }
        //解锁中级或者高级问卷
        TestSoulQuestion testSoulQuestion = testSoulQuestionMapper.selectById(questionId);
        TestSoul testSoul = testSoulMapper.selectById(testSoulQuestion.getTestSoulId());
        testScore.setTestId(testSoulQuestion.getTestSoulId());
        if (StrUtil.equals(testSoul.getLevel(),"初级")) {
            //如果是初级问卷，判断中级问卷是否解锁，如果没有则解锁中级问卷
            TestSoulLock testSoulLock = new TestSoulLock();
            //testSoulLock.setTestSoulId(Long.parseLong("500000197705041153"));
            LambdaQueryWrapper<TestSoulLock> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TestSoulLock::getLevel,"中级").eq(TestSoulLock::getUserId,userId);
            testSoulLock = testSoulLockMapper.selectOne(wrapper);
            if (testSoulLock.getIsLock()==1) {
                //未解锁中级，则解锁
                testSoulLock.setIsLock(0);
                testSoulLockMapper.updateById(testSoulLock);
            }
        }
        else if (StrUtil.equals(testSoul.getLevel(),"中级")) {
            //如果是中级问卷，则解锁高级问卷
            TestSoulLock testSoulLock = new TestSoulLock();
            //testSoulLock.setTestSoulId(Long.parseLong("500000197705041153"));
            LambdaQueryWrapper<TestSoulLock> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TestSoulLock::getLevel,"高级").eq(TestSoulLock::getUserId,userId);
            testSoulLock = testSoulLockMapper.selectOne(wrapper);
            if (testSoulLock.getIsLock()==1) {
                //未解锁高级，则解锁
                testSoulLock.setIsLock(0);
                testSoulLockMapper.updateById(testSoulLock);
            }
        }
        testScore.setSum(sum);
        return testScore;
    }
}
